/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package introse;

import java.util.ArrayList;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;

/**
 *
 * @author Rommel
 */
public class addTransactionProduct extends javax.swing.JPanel {

    public JPanel parentPanel;
    private List<product> productList;
    private float totalPrice;
    private boolean isActive;
    private transactionManager transMngr;
    private int quantity;

    public addTransactionProduct(List<product> productList, transactionManager transMngr) {
        initComponents();
        loadProductsBox(productList);
        totalPrice = 0;
        updateTotalPrice();
        this.transMngr = transMngr;
        this.isActive = true;
        quantity = 1;
    }

    public final void loadProductsBox(List<product> productList) {
        this.productList = productList;
        List<String> productNames = new ArrayList();

        for (product prod : this.productList) {
            productNames.add(prod.getProductName() + " (" + Integer.toString(prod.getCurrentStock()) + ")");
        }

        ComboBoxModel prodBoxModel = new DefaultComboBoxModel(productNames.toArray());
        productComboBox.setModel(prodBoxModel);
    }

    private void updateTotalPrice() {
        try {
            if (quantityBox.getText().isEmpty()) {
                quantityBox.setText("0");
            }

            quantity = Integer.parseInt(quantityBox.getText());
            totalPrice = quantity * productList.get(productComboBox.getSelectedIndex()).getPrice();
            quantityBox.setText(Integer.toString(quantity));
            priceLabel.setText(Float.toString(totalPrice) + " PHP");
        } catch (Exception ex) {
            quantityBox.setText(Integer.toString(quantity));
            updateTotalPrice();
        }
    }
    
    public float getTotalPrice(){
        if(getIsActive()){
            updateTotalPrice();
            return totalPrice;
        }else{
            return 0;
        }
    }

    public Boolean stockIsEnough() {
        if (productList.get(productComboBox.getSelectedIndex()).getCurrentStock() < Integer.parseInt(quantityBox.getText())) {
            return false;
        }
        return true;
    }

    public int getProductNumber() {
        return productList.get(productComboBox.getSelectedIndex()).getProductNumber();
    }

    public Boolean getIsActive() {
        return isActive;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        productComboBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        quantityBox = new javax.swing.JTextField();
        disposeButton = new javax.swing.JButton();
        priceLabel = new javax.swing.JLabel();

        productComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                productComboBoxItemStateChanged(evt);
            }
        });

        jLabel1.setText("Product:");

        jLabel2.setText("Quantity:");

        quantityBox.setText("1");
        quantityBox.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                quantityBoxKeyReleased(evt);
            }
        });

        disposeButton.setText("X");
        disposeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                disposeButtonActionPerformed(evt);
            }
        });

        priceLabel.setText("price");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(productComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(quantityBox, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(priceLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                .addComponent(disposeButton)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(productComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(disposeButton)
                    .addComponent(quantityBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(priceLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void addTransactionProduct() {
        if (isActive) {
            transMngr.addPurchasedProduct(productList.get(productComboBox.getSelectedIndex()).getProductNumber(), Integer.parseInt(quantityBox.getText()));
        }
    }

    private void disposeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_disposeButtonActionPerformed
        this.isActive = false;
        parentPanel.remove(this);
        parentPanel.revalidate();
        parentPanel.repaint();
    }//GEN-LAST:event_disposeButtonActionPerformed

    private void quantityBoxKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_quantityBoxKeyReleased
        updateTotalPrice();
    }//GEN-LAST:event_quantityBoxKeyReleased

    private void productComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_productComboBoxItemStateChanged
        updateTotalPrice();
    }//GEN-LAST:event_productComboBoxItemStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton disposeButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel priceLabel;
    private javax.swing.JComboBox productComboBox;
    private javax.swing.JTextField quantityBox;
    // End of variables declaration//GEN-END:variables
}
